package com.devunion.reader.server.service;

import com.devunion.bogdan.model.project.Page;
import com.devunion.bogdan.model.project.Paper;
import com.devunion.bogdan.model.project.Project;
import com.devunion.bogdan.model.project.dao.ProjectDao;
import com.devunion.reader.client.service.ProjectService;
import com.devunion.reader.client.to.PaperInfoTo;
import com.devunion.reader.client.to.ProjectTreeTo;
import com.devunion.reader.client.to.SearchProjectTo;
import com.devunion.reader.server.mediator.PageMediator;
import com.devunion.reader.server.mediator.PaperInfoMediator;
import org.gwtwidgets.server.spring.GWTSpringController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Timoshenko Alexander 19.08.2009 : 11:18:59
 */
public class ProjectServiceImpl extends GWTSpringController implements ProjectService {
    private ProjectDao projectDao;
    private PaperInfoMediator paperInfoMediator;
    private PageMediator pageMediator;

    public void setProjectDao(ProjectDao projectDao) {
        this.projectDao = projectDao;
    }

    public void setPaperInfoMediator(PaperInfoMediator paperInfoMediator) {
        this.paperInfoMediator = paperInfoMediator;
    }

    public void setPageMediator(PageMediator pageMediator) {
        this.pageMediator = pageMediator;
    }

    public List<ProjectTreeTo> getProjectTreeItems(ProjectTreeTo projectTreeTo, SearchProjectTo spt) {
        List<ProjectTreeTo> result = new ArrayList<ProjectTreeTo>();

        if (projectTreeTo == null) {
            Set<Project> projects = projectDao.getProjectByCriteria(spt.getFirstName(), spt.getLastName(), spt.getProjectName(),
                    spt.getTitle(), spt.getType(), spt.getLanguage(), spt.getPagesMin(), spt.getPagesMax(),
                    spt.getCreationMin(), spt.getCreationMax(), spt.getExactMin(), spt.getExactMax(), spt.getEndMin(),
                    spt.getEndMax(), spt.getAnnotationType(), spt.getAnnotationText());
            if (projects != null && !projects.isEmpty()) {
                for (Project project : projects) {
                    ProjectTreeTo to = new ProjectTreeTo();
                    to.setId(project.getProjectId());
                    to.setName(project.getName());
                    to.setHasChildren(true);

                    result.add(to);
                }
            }
        } else if (projectTreeTo.getPaperId() == null) {
            Set<Paper> entries = projectDao.getProjectPapersByCriteria(projectTreeTo.getId(), spt.getFirstName(), spt.getLastName(),
                    spt.getTitle(), spt.getType(), spt.getLanguage(), spt.getPagesMin(), spt.getPagesMax(),
                    spt.getCreationMin(), spt.getCreationMax(), spt.getExactMin(), spt.getExactMax(), spt.getEndMin(),
                    spt.getEndMax(), spt.getAnnotationType(), spt.getAnnotationText());

            if (entries != null) {
                for (Paper paper : entries) {
                    PaperInfoTo infoTo = paperInfoMediator.copyFromEntity(paper.getInfo());

                    ProjectTreeTo to = new ProjectTreeTo();
                    to.setId(projectTreeTo.getId());
                    to.setPaperId(paper.getPaperId());
                    to.setName(infoTo.getTitle());
                    to.setHasChildren(true);
                    to.setPaperInfoTo(infoTo);
                    to.setProjectName(projectTreeTo.getName());
                    result.add(to);
                }
            }
        } else {
            List<Page> pages = projectDao.loadPages(projectTreeTo.getId(), projectTreeTo.getPaperId(),
                    spt.getAnnotationType(), spt.getAnnotationText());
            if (pages != null && !pages.isEmpty()) {
                for (Page page : pages) {
                    if (page != null) {
                        ProjectTreeTo to = pageMediator.copyFromEntity(page);
                        to.setName(projectTreeTo.getName());
                        to.setProjectName(projectTreeTo.getProjectName());
                        result.add(to);
                    }
                }
            }
        }

        return result;
    }
}